/*
	后台-评论控制器
*/
'use strict';
var moment		= require('moment');
var AV 				= require('leanengine');
var mdCommon	= require('../modules/common');

var avComment 		= AV.Object.extend("Comment");

/**
 * 列表页面
 * 2015.04.18  by 胡斌
 */
exports.listPage = function(req, res) {
	var pager,where	= " objectId is exists",urlParam="",param={};
	var param1			= ["objectId"];
	var param2			= ["enabled"];
	var search			= req.query.search;

	for(var i=0;i<param1.length;i++){
		var val	= req.query[param1[i]];
		if(typeof(val)!="undefined" && val!=null){
			where	+= " and "+param1[i]+"='"+val+"'";
			param[param1[i]]=val;
			urlParam+="&"+param1[i]+"="+val;
		}
	}

	for(var i=0;i<param2.length;i++){
		var val=null;
		//console.log(param2[i]+"="+req.query[param2[i]]);
		if(req.query[param2[i]]=="true"){
			val=true;
		}else if(req.query[param2[i]]=="false"){
			val=false;
		}
		//console.log("typeof("+param2[i]+")="+typeof(val));
		if(typeof(val)=="boolean"){
			where	+= " and "+param2[i]+"="+val;
			param[param2[i]]=val;
			urlParam+="&"+param2[i]+"="+val;
		}
	}


	if(typeof(search)!="undefined" && search.length>0){
		//console.log(search);
		search=search.replace(/(^\s*)|(\s*$)/g, "");
		if(search.length>0){
			where	+= " and (content like '%"+search+"%')";
		}
		param['search']=search;
		urlParam+="&search="+search;
	}
	//console.log('select count(*) from _User where'+where);
	AV.Query.doCloudQuery('select count(*) from Comment where'+where).then(function(data){
		pager		= mdCommon.Paginate(req.query.page, req.query.pagesize, data.count);
		return AV.Query.doCloudQuery('select include trip,include image,include toUser,include fromUser,* from Comment where'+where+' limit '+(pager.currentPage-1)*pager.pagesize+','+pager.pagesize);
	}).then(function(data) {
		res.render('admin-comment-list.html',{
			user:req.AV.user,
			results:data.results,
			moment:moment,
			pager:pager,
			urlParam:urlParam,
			param:param
		});
	},function(error) {
		mdCommon.sentErr(res,req,error);
	});
}

/**
 * 删除图片
 * 2015.05.02  by 胡斌
 */
exports.doDelete = function(req, res) {
	var ids					= req.body.ids?req.body.ids:req.body.id;
	var queryComment 	= new AV.Query(avComment);
	if(typeof(ids)=="undefined")ids="-1";
	var idArr				= ids.split(",");

	queryComment.containedIn("objectId",idArr);
	queryComment.destroyAll().then(function(results) {
		if(req.body.ajax){
			res.json({success:true});
		}else{
			res.redirect('/admin/comment/list');
		}
  },function(error) {
		mdCommon.sentErr(res,req,error);
	});
}

exports.doSet = function(req, res) {
	var ids					= req.body.ids;
	var queryComment 	= new AV.Query(avComment);
	if(typeof(ids)=="undefined")ids="-1";
	var idArr				= ids.split(",");
	var type				= req.body.type;
	console.log(ids);
	queryComment.containedIn("objectId",idArr);
	queryComment.find().then(function(results){
		var enabled;
		if(type=="open"){
			enabled=true;//
		}else if(type=="ban"){
			enabled=false;//
		}
		if(typeof(enabled)=="boolean"){
			var promises = [];
			for(var i=0;i<results.length;i++){
				results[i].set("enabled",enabled);
				promises.push(results[i].save());
			}
			return AV.Promise.when(promises);
		}else{
			return AV.Promise.error('错误的指令！');//
		}
	}).then(function(results) {
		if(req.body.ajax){
			res.json({success:true});
		}else{
			res.redirect('/admin/comment/list');
		}
  },function(error) {
		mdCommon.sentErr(res,req,error);
	});
}
